package com.uyaer.wheel
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.game.Game;
	import com.uyaer.wheel.menu.Menu;
	
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import starling.display.Sprite;
	import starling.events.Event;
	
	/**
	 *中控制器 
	 * @author hc
	 * 
	 */	
	public class Main extends Sprite
	{
		private var menu:Menu;
		private var game:Game;
		public function Main()
		{
			if(stage){
				onAdded();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
		}
		
		private function onAdded():void{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			
			stage.addEventListener(EventType.START_GAME,onStartGame);
			stage.addEventListener(EventType.DESTORY_GAME,onDestoryGame);
			Common.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
			
			//打开menu
			menu = new Menu();
			addChild(menu);
		}
		
		private function onKeyDown(e:KeyboardEvent):void{
			if(e.keyCode == Keyboard.BACK){
				e.preventDefault();
			}
		}
		
		private function onDestoryGame(e:Event):void{
			if(game){
				game.destory(e.data as Boolean);
				game.removeFromParent(true);
				game = null;
			}
		}
		
		private function onStartGame():void{
			game = new Game();
			addChildAt(game,0);
		}
	}
}